<?php
if(!defined('DCS_ROOT')) exit('Access Denied');

class main {

	function __construct() {		
		$url = getUrl();		
		$action = $url['params']['1'];			
		if($action == 'collect'){
			$this->collectAction();
		}elseif($action == 'wish'){
			$this->wishAction();
		}elseif($action == 'reviews'){
			$this->reviewAction();
		}elseif($action == 'friend'){
			$this->friendAction();
		}elseif(is_numeric($url['action'])){
			$this->peopleAction();
		}else{
			pageError();
		}
		exit();
	}

	function defaultAction(){
		$url = getUrl();
		$user = $this->user;
		$tpl = new Template('people/main.htm');
		$DB = getDB();
		require_once(APP_PATH."model/people.php");

		$tpl->assign(getMember());
		$tpl->output();
	}

	function peopleAction (){
		$url = getUrl();
		$tpl = new Template('people/main.htm');
		$DB = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($DB);
		$uid = $url['action'];
		$collects = $data->getComments($uid,'1','c.addtime',10);
		$tpl->assign('collects',$collects);

		$wishs = $data->getComments($uid,'2','c.addtime',10);
		$tpl->assign('wishs',$wishs);

		$reviews = $data->getReviews($uid,'r.addtime',5);
		foreach($reviews as $k=>$v){
			$reviews[$k]['contents'] = csubstr(html2Txt($v['contents']),120);
			$reviews[$k]['addtime'] = date('m月d日',$v['addtime']);
		}
		$tpl->assign('reviewList',$reviews);

		$friends = $data->getFriends($uid,'f.times',10);
		foreach($friends as $k=>$v){
			$friends[$k]['avatars'] = empty($v['avatars']) ? 'default.gif' : $v['avatars'];  //$k, $v是什么意思
			$friends[$k]['uid'] = empty($v['uid']) ? 'default.gif' : $v['uid'];  // 有问题？？
		}
		$tpl->assign('friendList',$friends);

		

		$userInfo = $data->getUserbasic($uid);  //取url上的用户id，不一定是用户自己的，从上面可以看出来
		$myuid = getMember('uid');  //getmember函数是在funs定义，在这个数组包含用户自己的用户名、id
		if($myuid){
			if($myuid != $uid){  
				$is_contact = $data->isContact($myuid,$uid);
				if($is_contact){
					$userInfo['is_contact'] = '<a href="/people/contact/cancel/'.$uid.'" class="link1">取消关注</a>';
				}else{
					$userInfo['is_contact'] = '<a href="/people/contact/add/'.$uid.'" class="link1">关注此人</a>';
				}
			}else{
				$userInfo['username'] = '我';
			}
		}		
		$userInfo['avatars'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['avatars'];
		$userInfo['profile_image_url'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['profile_image_url'];  //本行新增，用于取新浪头像url
		$userInfo['user_url'] =  $userInfo['user_url'];  //本行新增,取用户新浪链接
		$tpl->assign($userInfo);  //用户自己的头像
		$tpl->assign(assignUser());
		$tpl->output();
	}

	function collectAction (){  //看过的电影
		$url = getUrl();
		$tpl = new Template('people/collect.htm');
		$db = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($db);

		$uid = $url['action'];		
		if($url['params']['2'] == 'page'){
			$pagetemp = $url['params']['3'];		
		}
		$param['table'] = ' movie_comments AS c,movie AS m ';
		$param['field'] = ' c.mid,c.rating,c.contents,c.addtime,m.title,m.thumb ';
		$param['where'] = " c.uid='$uid' AND c.mid=m.id AND c.type='1' ";
		$param['order'] = 'c.addtime';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '20';
		$param['pageUrl'] = '/people/'.$uid.'/collect/page/';
		$param['pageTotal']	= $db->getTotal($param);		
		$collects = $db->getList($param);		
		foreach($collects as $k=>$v){
			$collects[$k]['addtime'] = date('Y-m-d',$v['addtime']);
		}
		$tpl->assign('collects',$collects);
		$userInfo = $data->getUserbasic($uid);
		$userInfo['avatars'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['avatars'];
		$userInfo['total'] = $param['pageTotal'];
		$tpl->assign($userInfo);		
		$tpl->assign("pages", makePage($param));
		$tpl->assign(assignUser());
		$tpl->output();
	}

	function wishAction (){
		$url = getUrl();
		$tpl = new Template('people/wish.htm');
		$db = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($db);

		$uid = $url['action'];		
		if($url['params']['2'] == 'page'){
			$pagetemp = $url['params']['3'];		
		}
		$param['table'] = ' movie_comments AS c,movie AS m ';
		$param['field'] = ' c.mid,c.rating,c.contents,c.addtime,m.title,m.thumb ';
		$param['where'] = " c.uid='$uid' AND c.mid=m.id AND c.type='2' ";
		$param['order'] = 'c.addtime';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '20';
		$param['pageUrl'] = '/people/'.$uid.'/wish/page/';
		$param['pageTotal']	= $db->getTotal($param);		
		$wishs = $db->getList($param);		
		foreach($wishs as $k=>$v){
			$wishs[$k]['addtime'] = date('Y-m-d',$v['addtime']);
		}
		$tpl->assign('wishs',$wishs);
		$userInfo = $data->getUserbasic($uid);
		$userInfo['avatars'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['avatars'];
		$userInfo['total'] = $param['pageTotal'];
		$tpl->assign($userInfo);		
		$tpl->assign("pages", makePage($param));
		$tpl->assign(assignUser());
		$tpl->output();
	}

	function reviewAction (){
		$url = getUrl();
		$tpl = new Template('people/reviews.htm');
		$db = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($db);

		$uid = $url['action'];		
		if($url['params']['2'] == 'page'){
			$pagetemp = $url['params']['3'];		
		}

		$param['table'] = ' movie_review AS r,member AS m,movie AS m2  ';
		$param['field'] = ' r.id,r.uid,r.mid,r.rating,r.title,r.contents,r.reply,m.username,m.avatars,m2.title mtitle,m2.thumb ';
		$param['where'] = " r.uid = '$uid' AND r.uid = m.uid AND r.mid = m2.id ";
		$param['order'] = ' r.addtime ';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '20';
		$param['pageUrl'] = '/people/'.$uid.'/reviews/page/';
		$param['pageTotal']	= $db->getTotal($param);		
		$reviews = $db->getList($param);		
		foreach($reviews as $k=>$v){
			$reviews[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			$reviews[$k]['contents'] = csubstr(html2Txt($v['contents']),350,1);
		}
		$tpl->assign('reviewList',$reviews);
		$userInfo = $data->getUserbasic($uid);
		$userInfo['avatars'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['avatars'];
		$userInfo['total'] = $param['pageTotal'];
		$tpl->assign($userInfo);		
		$tpl->assign("pages", makePage($param));
		$tpl->output();
	}

	function friendAction (){
		$url = getUrl();
		$tpl = new Template('people/friend.htm');
		$db = getDB();
		require_once(APP_PATH."model/people.php");
		$data = new peopleModel($db);

		$uid = $url['action'];		
		if($url['params']['2'] == 'page'){
			$pagetemp = $url['params']['3'];		
		}

		$param['table'] = ' member_friend AS f,member AS m ';
		$param['field'] = ' f.uid,m.username,m.avatars ';
		$param['where'] = " f.uid = '$uid' AND f.fid = m.uid ";
		$param['order'] = ' f.times ';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '20';
		$param['pageUrl'] = '/people/'.$uid.'/friend/page/';
		$param['pageTotal']	= $db->getTotal($param);		
		$friends = $db->getList($param);
		foreach($friends as $k=>$v){
			$friends[$k]['avatars'] = empty($v['avatars']) ? 'default.gif' : $v['avatars'];
		}

		$tpl->assign('friends',$friends);

		$userInfo = $data->getUserbasic($uid);
		$userInfo['avatars'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['avatars'];
		$userInfo['total'] = $param['pageTotal'];
		$tpl->assign($userInfo);		

		$tpl->assign("pages", makePage($param));
		$tpl->assign(assignUser());
		$tpl->output();
	}
}
?>
